package com.chenfy.touchtransfer_android.tmp;

import android.Manifest;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.wifi.p2p.WifiP2pManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;

import com.chenfy.ioc_annotation.BindLayout;
import com.chenfy.ioc_api.Binding;
import com.chenfy.touchtransfer_android.R;
import com.chenfy.touchtransfer_android.base.BaseActivity;
import com.chenfy.ioc_annotation.BindView;

/**
 * Created by ChenFengyao
 * Date: 20-11-10
 */
@BindLayout(R.layout.activity_client)
public class ClientActivity extends BaseActivity implements WifiP2pManager.ChannelListener {

    private WifiP2pManager mWifiP2pManager;
    private WifiP2pManager.Channel mChannel;

    @BindView(R.id.client_search)
    public TextView mSearchTv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mWifiP2pManager = (WifiP2pManager) getSystemService(WIFI_P2P_SERVICE);
        mChannel = mWifiP2pManager.initialize(this, getMainLooper(), this);
        Binding.getInstance().bind(this);

        Intent p2pSettings = new Intent();
        p2pSettings.setComponent(new ComponentName("com.android.settings", "com.android.settings.Settings$WifiP2pSettingsActivity"));
        try {
            startActivity(p2pSettings);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onChannelDisconnected() {

    }

    @Override
    public void doClick(View v) {
        super.doClick(v);
        switch (v.getId()) {
            case R.id.client_search:
                Toast.makeText(this, "aaa", Toast.LENGTH_SHORT).show();
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(this, "权限没有", Toast.LENGTH_SHORT).show();
                    return;
                }
                mWifiP2pManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
                    @Override
                    public void onSuccess() {
                        Log.d("ClientActivity", "success");
                    }

                    @Override
                    public void onFailure(int reason) {
                        Log.d("ClientActivity", "reason:" + reason);
                    }
                });
        }
    }
}
